/*
 *  Copyright (c) 2015. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by Lucien.Chen on 2016/9/1.
 */
public class GrrGroupSettingsValidator implements ConstraintValidator<GrrGroupSettings, SysGrrDto> {

    @Override
    public void initialize(GrrGroupSettings grrGroupSettings) {
    }

    @Override
    public boolean isValid(SysGrrDto sysGrrDto, ConstraintValidatorContext constraintValidatorContext) {
        if (sysGrrDto == null) {
            return false;
        } else {
            String[] keys = {
                sysGrrDto.getGroup1Key(), sysGrrDto.getGroup2Key(), sysGrrDto.getPartKey(), sysGrrDto.getOperatorKey()
            };
            return compare(keys);
        }
    }

    private boolean compare(String[] key) {
        for (int i = 0; i < key.length; i++) {
            for (int j = i + 1; j < key.length; j++) {
                if (key[i].equals(key[j])) {
                    return false;
                }
            }
        }
        return true;
    }
}
